package com.longtop.perform.khyg.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.longtop.framework.base.BaseService;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.util.BeanUtil;
import com.longtop.framework.util.SpringBeanFactory;
import com.longtop.framework.util.StringUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.common.Constants;
import com.longtop.intelliweb.view.Dataset;
import com.longtop.intelliweb.view.Record;
import com.longtop.perform.khyg.model.TKhygIndex;
import com.longtop.perform.khyg.model.TKhygIndexData;
import com.longtop.perform.khyg.model.TKhygSegment;

/**
 * 
 * @author 孙景弘
 * 
 */
public class IndexDataService extends BaseService {

	public static final String BEAN_ID = "indexDataService";

	/**
	 * 组装员工-定性指标明细DATASET
	 * 
	 * @param dataset_project_index
	 * @param dataset_task_user
	 */
	public void bulidUserDxIndexDataset(Dataset dataset_project_index, Dataset dataset_task_user) {
		List<TKhygIndex> indexlist = BeanUtil.datasetToList(dataset_project_index, TKhygIndex.class);
		for (TKhygIndex projectIndex : indexlist) {
			// 指标ID为fieldName 指标名称为filedLabel 。-fieldValue是员工任务对应的任务值taskValue
			String fieldName = projectIndex.getIndexId();
			dataset_task_user.addField(fieldName, Constants.DataType_BIGDECIMAL);
			dataset_task_user.getField(fieldName).setLabel(projectIndex.getIndexName());
			dataset_task_user.getField(fieldName).setMaxLength(14);
			dataset_task_user.getField(fieldName).setScale(2);
		}
	}

	/**
	 * 保存员工-主观评分（定性指标）
	 * 
	 * @param indexList
	 * @param taskUserRecordList
	 */
	public void saveUserIndexData(List<TKhygIndex> indexList, List<Record> taskUserRecordList, SessionObject sessionObject) {
		String segmentId = "";
		// 组装页面显示DATASET-到UserIndexData：
		for (TKhygIndex TKhygIndex : indexList) {
			for (Record record : taskUserRecordList) {
				String userId = record.getValue("userId").toString();
				segmentId = record.getValue("segmentId").toString();
				String indexId = TKhygIndex.getIndexId();
				BigDecimal indexValue = (BigDecimal) record.getValue(indexId);// 员工定性指标值indexValue

				// 取得员工对应的定性指标数据
				List<TKhygIndexData> tmpList = this.queryAll("from TKhygIndexData where userId = '" + userId + "' and indexId = '" + indexId + "'");
				TKhygIndexData TKhygIndexData = tmpList.get(0);

				if (TKhygIndexData.getIndexValue() == null || indexValue == null || indexValue.doubleValue() != TKhygIndexData.getIndexValue().doubleValue()) {
					TKhygIndexData.setIndexValue(indexValue); // 设置定性指标值
					TKhygIndexData.setDataSource("HAND"); // 设置数据来源为手工输入
					this.setAuthPropetryToEntity(TKhygIndexData, sessionObject, false);// 设置更新权限属性
					super.update(TKhygIndexData); // 更新员工定性指标
				}
			}
		}
		// 修改相应考核分段表的状态:
		ProjectService projectService = SpringBeanFactory.getBean(ProjectService.BEAN_ID);
		projectService.updateSegmentStatus(TKhygSegment.class, segmentId, 5, true);
		projectService.updateSegmentStatus(TKhygSegment.class, segmentId, 6, false);// 计算成绩状态为0
	}

	/**
	 * 获取考核员工对应定性指标
	 * 
	 * @param segmentId
	 * @param dataset_task_user
	 * @param context
	 * @return
	 */
	public Dataset getUserIndexData(String segmentId, Dataset dataset_task_user, IActionContext context) {
		// 取得考核分段-任务对应的员工
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("segmentId", segmentId);

		if (context != null) {
			String userIdListStr = context.getElementValue("Text_userIdListStr");
			userIdListStr = StringUtil.afterOfDeletion(userIdListStr, ",");// 员工ID字符串，用逗号隔开
			queryMap.put("userIdListStr", userIdListStr);
		}

		this.queryForPageByIbatis("khygTaskSql.queryTaskUser", null, queryMap, dataset_task_user, false);
		// 取得考核阶段-指标明细表（定性）
		List<TKhygIndexData> TKhygIndexDataList = this.queryAll("from TKhygIndexData where segmentId = '" + segmentId + "' and indexType = '1'");

		// 填充-定性指标明细DATASET
		List<Record> taskUserRecordList = dataset_task_user.getRecordList();
		for (Record record : taskUserRecordList) {
			for (TKhygIndexData TKhygIndexData : TKhygIndexDataList) {
				if (record.getValue("userId").equals(TKhygIndexData.getUserId()) && record.getValue("segmentId").equals(TKhygIndexData.getSegmentId())) {
					record.setValue(TKhygIndexData.getIndexId(), TKhygIndexData.getIndexValue());
				}
			}
		}
		return dataset_task_user;
	}
}